package top.someapp.applib.base;

import top.someapp.applib.Result;

/**
 * @author zwz
 * Created on 2020-03-30
 */
public class DefaultResult implements Result<Object> {

    public static final int CODE_FORBIDDEN = 403;
    public static final int CODE_AWAIT = -1;
    private boolean success;
    private int code;
    private String message;
    private Object data;
    private transient boolean end;

    public DefaultResult() {
        // default constructor
    }

    public DefaultResult(boolean success) {
        this.success = success;
    }

    public static Result success(String message) {
        return success(message, null);
    }

    public static Result success(String message, Object data) {
        return new DefaultResult(true).setMessage(message).setData(data);
    }

    public static Result error(String message) {
        return new DefaultResult(false).setMessage(message);
    }

    public static Result error(int code) {
        return new DefaultResult(false).setCode(code);
    }

    @Override
    public boolean isSuccess() {
        return success;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public Result setCode(int code) {
        this.code = code;
        return this;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    @Override
    public Object getData() {
        return data;
    }

    @Override
    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    @Override
    public Result end(boolean success) {
        if (!end) {
            this.success = success;
            end = true;
        }
        return this;
    }
}
